using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Models.Repositories
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	#endregion
	public class ThongTinCongTyRepository
	{
		public List<ThongTinCongTy> SelectAll()
		{
			return DataContext.Instance.ThongTinCongTies.ToList();
        }

		public ThongTinCongTy SelectByID(int id)
		{
			return DataContext.Instance.ThongTinCongTies.FirstOrDefault(item => item.ID == id );
		}

		public List<ThongTinCongTy> SelectPaging(int page, int pageSize)
		{
			var lstItem = DataContext.Instance.ThongTinCongTies.ToList();
            //if (pageSize > 0)
            //{
            //    int startIndex, itemCount;
            //    startIndex = (page - 1) * pageSize;
            //    itemCount = pageSize;
            //    if (startIndex + itemCount > lstItem.Count)
            //        itemCount = lstItem.Count - startIndex;
            //    lstItem = lstItem.GetRange(startIndex, itemCount);
            //}
			return lstItem;
		}

		public bool Insert(ThongTinCongTy obj)
		{
			try
			{
				DataContext.Instance.ThongTinCongTies.InsertOnSubmit(obj);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.ThongTinCongTies.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.ThongTinCongTies.DeleteOnSubmit(delitem);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			return DataContext.Instance.ThongTinCongTies.Count();
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


		public List<ThongTinCongTy> RetrieveByID(int id)
		{
			return (from item in DataContext.Instance.ThongTinCongTies where  item.ID == id  select item).ToList();
		}

	}
}